package com.smart.home.util;

import com.smart.home.command.EnvironmentCommand;
import com.smart.home.constant.Constant;

import java.util.HashMap;
import java.util.Map;

/**
 * 设备返回码解析工具
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-10
 */
public abstract class ResultCodeResolver {

    /**
     * 正数编码
     */
    private static final String POSITIVE = "01";
    /**
     * 设防
     */
    private static final String DEFENSE = "01";
    /**
     * 撤防
     */
    private static final String UN_DEFENSE = "00";

    /**
     * 有线输出状态判断
     *
     * @param result 有线输出返回字符串
     * @return true-成功，false-失败
     */
    public static boolean wireOutputResultStatus(String result) {
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        String wireOutputSuccess = Constant.WIRE_OUTPUT_SUCCESS;
        String statusCode = result.substring(result.length() - wireOutputSuccess.length());
        return wireOutputSuccess.equals(statusCode);
    }

    /**
     * 有线输入状态判断
     *
     * @param result 有线输出返回字符串
     * @return true-成功，false-失败
     */
    public static boolean wireInputResultStatus(String result) {
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        return result.equals(Constant.WIRE_INPUT_SUCCESS);
    }

    /**
     * 无线按钮执行返回状态
     *
     * @param result 无线按钮执行结果返回字符串
     * @return true-成功，false-失败
     */
    public static boolean wirelessExecuteResultStatus(String result) {
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        return result.equals(Constant.WIRELESS_EXE_SUCCESS);
    }

    /**
     * 情景模式按钮执行返回状态
     *
     * @param result 返回编码
     * @return true-成功，false-失败
     */
    public static boolean contextualExecuteResultStatus(String result) {
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        return result.equals(Constant.CONTEXTUAL_EXE_SUCCESS);
    }

    /**
     * 条件控制执行结果解析
     *
     * @param result 返回编码
     * @return true-成功，false-失败
     */
    public static boolean conditionExecuteResultStatus(String result) {
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        return result.equals(Constant.CONDITION_EXE_SUCCESS);
    }

    /**
     * 温湿度、设防信息解析
     *
     * @param result 室内环境返回结果
     * @return key-对应数据名，val-具体数据值
     */
    public static Map<String, Object> indoorEnvironmentResultResolver(String result) {
        if (StringUtils.isEmpty(result)) {
            return null;
        }
        Map<String, Object> resultMap = new HashMap<>();
        //温度
        String temperatureCode = result.substring(2, 6);
        Integer temperature = StringUtils.hexToInt(temperatureCode.substring(2));
        String symbol = temperatureCode.substring(0, 2);
        if (!POSITIVE.equals(symbol)) {
            //负数处理
            resultMap.put(EnvironmentCommand.TEMPERATURE, -temperature);
        } else {
            resultMap.put(EnvironmentCommand.TEMPERATURE, temperature);
        }
        //湿度
        String humidity = result.substring(6, 10);
        resultMap.put(EnvironmentCommand.HUMIDITY, StringUtils.hexToInt(humidity.substring(2)));
        //是否布防
        String defense = result.substring(10, 12);
        if (DEFENSE.equals(defense)) {
            resultMap.put(EnvironmentCommand.DEFENSE, true);
        }
        if (UN_DEFENSE.equals(defense)) {
            resultMap.put(EnvironmentCommand.DEFENSE, false);
        }
        return resultMap;
    }

}
